const api = require('../../utils/api.js');
const app = getApp();

Page({
  data: {
    list: [],
    theme: app.themeData,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-17.png',
      text: '暂无可用优惠券',
    },
    unit: '',
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    page: 1,
    is_more: true,
    isShowLoading: false,
  },

  onLoad(options) {
    let opts = options || {};
    let that = this;
    let isMulti =
      app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isMulti; // 是否为多店
    app.pageOnLoad(this);
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    this.setData({
      status: opts.status || -1,
      opts,
      isMulti,
      useTabbar: this.data.useTabbar && app.getTabBarStatus(),
    });
    this.loadData();
  },

  loadData() {
    let that = this;
    let { is_more, list, page } = that.data;
    if (is_more) {
      wx.showLoading({
        title: '加载中',
        mask: true,
      });
      app.ws_micro_shoprequest({
        url: api.coupon.index,
        data: {
          status: that.data.status,
          page: page
        },
        success: function(res) {
          if (res.code === 0) {
            let new_list = res.data.list || [];
            if (new_list.length > 0) {
              page += 1;
              that.setData({
                list: list.concat(new_list),
                page,
              });
            } else {
              that.setData({
                is_more: false,
              });
            }
          } else {
            wx.showToast({
              title: res.msg || '',
              icon: 'none',
            });
          }
        },
        complete: function() {
          wx.hideLoading();
          that.setData({
            isShowLoading: true,
          })
        },
      });
    }
  },

  onReachBottom() {
    this.loadData();
  },

  switchList(e) {
    let status = e.currentTarget.dataset.status;
    this.setData({
      status,
      page: 1
    });
    this.loadData();
  },

  collectFormId(e) {
    app.collectFormId(e);
  },

  touseEvent1(e) {
    console.log('touseEvent1', e.detail.id);
    app.util.navigateTo('/ws_micro_shop/pages/list/list?integral=0');
  },
});
